﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using TierApplication;

namespace OrderMgr.OrderInfo
{
    public partial class frmBuyToStockDetailProduct : TierService.frmTemplate
    {
        public TierService.Utility.OperateMode OpMode;
        public string _POrderID;
        public string _POrderDetailID;
        public string _OrderID;
        public string _ProductID;
        public string _OrderDetailID;

        public frmBuyToStockDetailProduct()
        {
            InitializeComponent();
        }

        private void frmOrderProductDetail_Load(object sender, EventArgs e)
        {
            switch (OpMode)
            {
                case Utility.OperateMode.Insert:
                    txtPOrderID.Text = _POrderID;
                    txtOrderID.Text = _OrderID;
                    txtProductID.Text = _ProductID;

                    queryInfo(_POrderID, _ProductID);
                    break;
                case Utility.OperateMode.Query:
                    break;
                case Utility.OperateMode.Update:
                    queryInfoByOrderDetailID(_OrderDetailID);
                    break;

                default:
                    break;
            }
            this.Text = "入库明细录入";
        }

        private void queryInfo(string @ac_BuyOrderID, string sProductID)
        {
            DataSet ds = T_Order.GetBuyOrderDetail(@ac_BuyOrderID, sProductID);
            DataRow dr = ds.Tables[0].Rows[0];
            txtProductName.Text = dr["ProductName"].ToString();
            txtProductNo.Text = dr["ProductNo"].ToString();
            txtActQuantity.Text = dr["ActQuantity"].ToString();
            txtStockQuantity.Text = dr["StockQuantity"].ToString();
            txtStockBckQuantity.Text = dr["StockBckQuantity"].ToString();

            T_Product mProduct = new T_Product();
            mProduct.GetModel(sProductID);
            txtUsefulLifeType.EditValue = mProduct.UsefulLifeType;
            txtBasePlace.Text = mProduct.BaseProductPlace;
            txtProductType.Text = mProduct.ProductTypeName;
            
        }

        private void queryInfoByOrderDetailID(string sOrderDetailID)
        {
            T_OrderDetail mOrderDetail = new T_OrderDetail();
            mOrderDetail.GetModel(sOrderDetailID);

            txtProductName.Text = mOrderDetail.ProductName;
            txtProductNo.Text = mOrderDetail.ProductNo;
            txtOrderID.Text = mOrderDetail.OrderID;
            txtProductID.Text = mOrderDetail.ProductID;

            txtActQty.Value = mOrderDetail.ActQuantity.Value;
            txtBatchNo.Text = mOrderDetail.BatchNo;
            txtDepotArea.Text = mOrderDetail.DepotArea;
            txtUsefulLifeType.EditValue = mOrderDetail.UsefulLifeType;
            if (mOrderDetail.UsefulLife != null)
                txtUsefulLife.DateTime = mOrderDetail.UsefulLife.Value;

            DataSet ds = T_Order.GetBuyOrderDetail(mOrderDetail.OrderDetailID);
            DataRow dr = ds.Tables[0].Rows[0];
            txtProductName.Text = dr["ProductName"].ToString();
            txtProductNo.Text = dr["ProductNo"].ToString();
            txtActQuantity.Text = dr["ActQuantity"].ToString();
            txtStockQuantity.Text = dr["StockQuantityE"].ToString();
            txtStockBckQuantity.Text = dr["StockBckQuantity"].ToString();


        }




        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            decimal dBuyAmt = txtActQuantity.Value;
            decimal dStockAmt = txtStockQuantity.Value;
            decimal dAct = txtActQty.Value;

            if (txtUsefulLifeType.Text == "")
            {
                MessageBox.Show("请输入有效期！");
                txtUsefulLifeType.Focus();
                return;
            }

            if ((txtUsefulLifeType.Text == "一次性")&&(txtUsefulLife.Text == ""))
            {
                MessageBox.Show("一次性商品请输入有效期！");
                txtUsefulLife.Focus();
                return;
            }

            if (txtBatchNo.Text == "")
            {
                MessageBox.Show("批号不能为空,请输入批号！");
                txtBatchNo.Focus();
                return;
            }

            if (dAct == 0)
            {
                MessageBox.Show("入库数量不能为0,请输入！");
                txtActQty.Focus();
                return;
            }

            if (dAct > dBuyAmt - dStockAmt)
            {
                MessageBox.Show("入库数量不正确,请重新录入（入库总数量不能大于采购数量-已入库数量)！");
                txtActQty.Focus();
                return;
            }

            //同类商品明细，同批号不能重复
            int iDm = 0;
            string sSm = "";

            T_Order.CheckBuyStock(txtOrderID.Text,_OrderDetailID, txtProductID.Text, txtBatchNo.Text, ref iDm, ref sSm);
            if (iDm < 0)
            {
                MessageBox.Show(sSm);
                return;
            
            }

            T_OrderDetail mOrderDetail = new T_OrderDetail();

            if (OpMode == Utility.OperateMode.Update)
            {
                mOrderDetail.GetModel(_OrderDetailID);
                _OrderID = mOrderDetail.OrderID;
                _ProductID = mOrderDetail.ProductID;
                _POrderDetailID = mOrderDetail.POrderDetailID;
            }
         
            mOrderDetail.POrderDetailID = _POrderDetailID;
            mOrderDetail.OrderID = _OrderID;
            mOrderDetail.ProductID = _ProductID;
            mOrderDetail.ProductName = txtProductName.Text;
            mOrderDetail.ProductNo = txtProductNo.Text;
            mOrderDetail.PlnQuantity = txtActQty.Value;
            mOrderDetail.ActQuantity = txtActQty.Value;
            mOrderDetail.BckQuantity = 0;
            mOrderDetail.BatchNo = txtBatchNo.Text;
            mOrderDetail.CreateDate = DateTime.Now;
            mOrderDetail.CreateUser = Utility.gUserID;
            mOrderDetail.DepotArea = txtDepotArea.Text;
            mOrderDetail.UsefulLifeType = txtUsefulLifeType.Text;
            mOrderDetail.ProductType = txtProductType.Text;
            mOrderDetail.BaseProductPlace = txtBasePlace.Text;

            if (txtUsefulLife.Text != "")
                mOrderDetail.UsefulLife = txtUsefulLife.DateTime;

            T_OrderDetail mPOrderDetail = new T_OrderDetail();
            mPOrderDetail.GetModel(_POrderDetailID);
            mOrderDetail.UnitPrice = mPOrderDetail.UnitPrice;
            mOrderDetail.SumPrice = Math.Round(mPOrderDetail.UnitPrice.Value * txtActQty.Value, 2);

            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mOrderDetail.OrderDetailID = Utility.GetSeqNextval(Utility.SeqName.OrderDetailID.ToString()).ToString();
                        //mOrderDetail.RelaID = mOrderDetail.GetOrderDetailRelaID(mOrderDetail.OrderDetailID, mOrderDetail.POrderDetailID);
                        mOrderDetail.RelaID = mOrderDetail.OrderDetailID;//
                        mOrderDetail.Add();
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    MessageBox.Show("商品信息新增成功!");
                    DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        // mCustomer.CustID = txtCustID.Text;
                        mOrderDetail.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("商品信息修改出错,错误原因为:" + ex.Message);
                        break;
                    }


                    MessageBox.Show("商品信息修改成功!");
                    DialogResult = DialogResult.OK;
                    break;
                default:
                    break;
            }
        }

        private void txtUsefulLifeType_EditValueChanged(object sender, EventArgs e)
        {
            if ((string)txtUsefulLifeType.EditValue == "长期")
            {
                txtUsefulLife.Visible = false;
            }
            else
            {
                txtUsefulLife.Visible = true;
            }
        }
    }

}